package com.example.commonlib.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;

import com.example.commonlib.commonutil.ActivityManager;
import com.example.commonlib.commonutil.MLog;
import com.example.commonlib.widget.autosize.AutoSize;
import com.example.commonlib.widget.autosize.ResourcesWrapper;

import butterknife.ButterKnife;


/**
 * Created by yuyue on 15/12/10.
 */
public abstract class BaseActivity extends AppCompatActivity implements BaseView{

    private static final String TAG = BaseActivity.class.getSimpleName();

    protected Activity mActivity;

    //切换用frgment相关
    protected FragmentManager fragmentManager;

    protected int currentViewId = -1;
    //今日头条适配方案
    private ResourcesWrapper mResources;
//    protected Fragment fragment = null;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //去除ActionBar
//        AutoLayoutConifg.getInstance().useDeviceSize();
//        LinkedME.getInstance().onLMCreated(this);
        setContentView();
        ButterKnife.bind(this);
        this.mActivity = this;
        fragmentManager = getSupportFragmentManager();
        ActivityManager.getInstance().addActivity(this);

        final View layout = findViewById(Window.ID_ANDROID_CONTENT);
        ViewTreeObserver vto = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                init(savedInstanceState);
                getData(savedInstanceState);
                setData(savedInstanceState);
                setLisitener(savedInstanceState);
            }
        });
    }

    protected abstract void setContentView();

    /**
     * 初始化
     */
    protected abstract void init(Bundle savedInstanceState);

    /**
     * 获取数据
     */
    protected void getData(Bundle savedInstanceState){

    }

    /**
     * 加载数据
     */
    protected void setData(Bundle savedInstanceState){

    }

    /**
     * 加载监听
     */
    protected void setLisitener(Bundle savedInstanceState){

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void showSuccess(String msg) {

    }

    @Override
    public void tokenFailed() {

    }

    /**
     * 切换fragment
     * @param resid   容器id
     * @param fragmentResId  fragmengt id
     */
    protected void changeFragment(int resid,int fragmentResId){
        changeFragment(resid, fragmentResId, false, null,0,0,0,0);
    }

    /**
     * 切换fragment
     * @param resid   容器id
     * @param fragmentResId  fragmengt id
     */
    protected void changeFragment(int resid,int fragmentResId,int inA,int outA){
        changeFragment(resid, fragmentResId, false, null,inA, outA,0,0);
    }

    /** 切换fragment
     * @param resid   容器id
     * @param fragmentResId  fragmengt id
     */
    protected void changeFragment(int resid,int fragmentResId,boolean isAddBackStack,String name){
        changeFragment(resid, fragmentResId, isAddBackStack, name,0,0,0,0);
    }


    protected void changeFragment(int resid, int fragmentResId, boolean isAddBackStack, String name, int inA, int outA, int inB, int outB){
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragment = getInstanceByIndex(fragmentResId);

        if (fragment == null){
            return;
        }
        if (inA != 0 && outA != 0)
            transaction.setCustomAnimations(inA,outA,inB,outB);
        transaction.replace(resid, fragment);
        transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

        if (isAddBackStack)
            transaction.addToBackStack(name);

        transaction.commitAllowingStateLoss();

        currentViewId = fragmentResId;
    }


    /**
     * 隐藏fragment
     * @param fragment
     */
    protected void hideFragment(Fragment fragment){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragment);
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     * @param fragmentTag
     */
    protected void hideFragment(int fragmentTag){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragmentManager.findFragmentById(fragmentTag));
        transaction.commitAllowingStateLoss();
    }


    /**
     * 切换fragment
     *
     * @param id fragmentId
     * @return  fragment
     */
    protected Fragment getInstanceByIndex(int id){
        return null;
    }


    @Override
    protected void onStart() {
//        LinkedME.getInstance().onLMStarted(this);
        super.onStart();
    }

    @Override
    protected void onResume() {
//        LinkedME.getInstance().onLMResumed(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
//        LinkedME.getInstance().onLMPaused(this);
        super.onPause();
    }

    @Override
    protected void onStop() {
//        LinkedME.getInstance().onLMStoped(this);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        ActivityManager.getInstance().removeActivity(mActivity);
//        LinkedME.getInstance().onLMDestoryed(this);
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public Resources getResources() {
        if (mResources == null) {
            final AutoSize autoSize = getAutoSize();
            if (autoSize != null) {
                mResources = new ResourcesWrapper(super.getResources(), autoSize);
            }
        }
        return mResources != null ? mResources : super.getResources();
    }


    @Nullable
    protected AutoSize getAutoSize() {
        return new AutoSize(375/*设计的宽度dp*/, true);
    }
}
